************************************************************
*****     Create final study sample		       *****
************************************************************

/* CONTENTS:
- Create outcome variables and controls by sample
- Final cleaning for summary statistics
- Create Analytical Datasets and Samples Corresponding to Main Regressions
- Define DiD Hospitalization Sample
- Define DiD Mortality Sample
- Transform data to run event study
*/

*********************************************************
* Create Outcome Variables and Controls by Sample 
*********************************************************

timer clear 1
timer on 1
set trace off
global names "2 3 4 4_week 4_month 4_skin 4_cancer 4_h_mean 4_l_mean _all  4_mortality 2_mortality_acc 3_mortality_acc 4_mortality_acc 4_mortality_acc_week 4_mortality_acc_month _mortality_all"  
global datasets "tf_all_2 tf_all_3 tf_all_4 tf_all_week_4 tf_all_month_4 tf_all_skin_4 tf_all_cancer_4 tf_all_h_4 tf_all_l_4 matched tf_all_mortality_4 tf_all_mortality_accident_2 tf_all_mortality_accident_3 tf_all_mortality_accident_4 tf_all_mortality_acc_week_4 tf_all_mortality_acc_month_4 matched_mortality"

local N : word count $names
forvalues i = 1/`N' {
	
	local name `: word `i' of $names'
	local dataset `: word `i' of $datasets'
	
	local did_vars treat ageinptreat ageinphospitalnew year_treat 
	local matchdata "matched"
	if `i' > 10 {
		local matchdata "matched_mortality"
		local did_vars treat ageinptreat agemortalitynew year_treat
	}
	
	local eventstudy = 0
	if inlist(`i', 10,17) {
		local eventstudy = 1
		local did_vars
	}
	
	local additional_outcomes = 0
	if inlist(`i', 3, 10, 11, 14, 17) {
		local additional_outcomes = 1
	}
	
	* Set max number of years ahead of the event we'd like to study
	local max_time 10
	
	{ // Main control and outcome creation 
		{ // * MOTHERS *
		* Merge individual information with treatment assignment
		use "$processed_data\mothers1988_2019_wide.dta", clear
		duplicates drop shnro_mother, force /*only missings*/
		cap drop _merge
		if `eventstudy' != 1 {
			merge 1:m shnro_mother using "$processed_data/`dataset'.dta" 
			drop if _merge==1 
			drop _merge
			** generate year of treat (treatmeent year and placebo year for control units)
			g year_treat = child_b_year + ageinptreat
			g yearinph=year_treat
			
		}
		else {
			merge 1:1 shnro_mother using "$processed_data/`dataset'.dta" 
			drop if _merge==1
			drop _merge
			cap g yearinph=year(first_inphospital)
			cap g yearinph=year(first_mortality)

		}
		
		
		* INCOME before/after hospitalization
		** Set annual income to 0 if missing income and occupation
		forvalues y=1988(1)2018{
			replace income`y'=0 if income`y'==. & occupational_status`y'==.
		}
		** Gen variables for income in each year w.r.t yearinph
		forvalues x=0(1)`max_time'{
			g income_year`x'=.
		}
		forvalues y=1988(1)2017{
			forvalues x= 0(1)`max_time'{
			local year=`y'+`x'
			if `y'<=(2018-`x'){
				replace income_year`x'=income`year' if yearinph==`y'
				}
			}
		}

		** Calculate income in 5 years before (to check pretrends)
		forvalues x=1(1)5{
			g income_yearminus`x'=.
		}
		forvalues y=1988(1)2018{
			forvalues x= 1(1)5{
			local year=`y'-`x'
			if `year'>=1988{
				replace income_yearminus`x'=income`year' if yearinph==`y'
				}
			}
		}

		* EMPLOYMENT before/after hospitalization
		** calculate extensive margin variables: working if occupational_status==1 or 2
		forvalues x=0(1)`max_time'{
			g working_year`x'=.
		}
		forvalues y=1988(1)2017{
			forvalues x= 0(1)`max_time'{
				local year=`y'+`x'
				if `y'<=(2018-`x'){
					replace working_year`x'=(occupational_status`year'!=.) ///
					if yearinph==`y'
				}
			}
		}

		** Calculate 5 years before (to check pretrends)
		forvalues x=1(1)5{
			g working_yearminus`x'=.
		}
		forvalues y=1988(1)2018{
			forvalues x= 1(1)5{
				local year=`y'-`x'
				if `year'>=1988{
					replace working_yearminus`x'=(occupational_status`year'!=.) ///
					if yearinph==`y'
				}
			}
		}

		* EDUCATION before/after hospitalization
		** Clean
		forvalues y=1988(1)2018{
			g educ`y'=substr(ututku_aste`y',1,1)
			destring educ`y', replace
			gen uni_educ`y'=(educ`y'>=5) if  educ`y'!=. & educ`y'!=9
		}
		** Create var for level of education in the year of first hospital admission
		gen educ0=.
		gen uni_educ0=.
		forvalues y=1988(1)2018{
			replace educ0=educ`y' if yearinph==`y'
			replace uni_educ0=uni_educ`y' if yearinph==`y'
		}
		
		
		* PROBABILITY of DIVORCE
		forvalues x=0(1)`max_time'{
			g divorced_year`x'=.
		}

		forvalues y=1988(1)2019{
			forvalues x= 0(1)`max_time'{
				local year=`y'+`x'
				if `y'<=(2015-`x'){
					replace divorced_year`x'=(divorced`year'==1)  if yearinph==`y'
				}
			}
		}
		
	   * PROBABILITY of MARRIED

		forvalues x=0(1)`max_time'{
			g married_year`x'=.
		}

		forvalues y=1988(1)2019{
			forvalues x= 0(1)`max_time'{
				local year=`y'+`x'
				if `y'<=(2015-`x'){
					replace married_year`x'=(married`year'==1)  if yearinph==`y'
				}
			}
		}
  * PROBABILITY of UNMARRIED

		forvalues x=0(1)`max_time'{
			g unmarried_year`x'=.
		}

		forvalues y=1988(1)2019{
			forvalues x= 0(1)`max_time'{
				local year=`y'+`x'
				if `y'<=(2015-`x'){
					replace unmarried_year`x'=(unmarried`year'==1)  if yearinph==`y'
				}
			}
		}
		
		  * PROBABILITY of WIDOWED

		forvalues x=0(1)`max_time'{
			g widowed_year`x'=.
		}

		forvalues y=1988(1)2019{
			forvalues x= 0(1)`max_time'{
				local year=`y'+`x'
				if `y'<=(2015-`x'){
					replace widowed_year`x'=(widowed`year'==1)  if yearinph==`y'
				}
			}
		}
		
		* family status loop:
	foreach var in	married_nokids married_kids mother_kids father_kids coh_commonkids coh_nocommonkids coh_nokids {
	
		forvalues x=0(1)`max_time'{
			g `var'_year`x'=.
		}

		forvalues y=1988(1)2019{
			forvalues x= 0(1)`max_time'{
				local year=`y'+`x'
				if `y'<=(2015-`x'){
					replace `var'_year`x'=(`var'`year'==1)  if yearinph==`y'
				}
			}
		}
		}
		
		** 5 years before (to check pretrends)
		forvalues x=1(1)5{
			g divorced_yearminus`x'=.
		}
		forvalues y=1988(1)2019{
			forvalues x= 1(1)5{
				local year=`y'-`x'
				if `year'>=1988{
					replace divorced_yearminus`x'=(divorced`year'==1) if yearinph==`y'
				}
			}
		}
		
			forvalues x=1(1)5{
			g married_yearminus`x'=.
		}
		forvalues y=1988(1)2019{
			forvalues x= 1(1)5{
				local year=`y'-`x'
				if `year'>=1988{
					replace married_yearminus`x'=(married`year'==1) if yearinph==`y'
				}
			}
		}
		
					forvalues x=1(1)5{
			g unmarried_yearminus`x'=.
		}
		forvalues y=1988(1)2019{
			forvalues x= 1(1)5{
				local year=`y'-`x'
				if `year'>=1988{
					replace unmarried_yearminus`x'=(unmarried`year'==1) if yearinph==`y'
				}
			}
		}
		
					forvalues x=1(1)5{
			g widowed_yearminus`x'=.
		}
		forvalues y=1988(1)2019{
			forvalues x= 1(1)5{
				local year=`y'-`x'
				if `year'>=1988{
					replace widowed_yearminus`x'=(widowed`year'==1) if yearinph==`y'
				}
			}
		}
 * family status loop 
	foreach var in	married_nokids married_kids mother_kids father_kids coh_commonkids coh_nocommonkids coh_nokids {
							forvalues x=1(1)5{
			g `var'_yearminus`x'=.
		}
		forvalues y=1988(1)2019{
			forvalues x= 1(1)5{
				local year=`y'-`x'
				if `year'>=1988{
					replace `var'_yearminus`x'=(`var'`year'==1) if yearinph==`y'
				}
			}
		}
	}

		* PROBABILITY of WORKING in the public sector
		forvalues x=0(1)`max_time'{
			g public_year`x'=.
		}
		forvalues y=1988(1)2018{
			forvalues x= 0(1)`max_time'{
				local year=`y'+`x'
				if `y'<=(2015-`x'){
					replace public_year`x'=(public`year'==1)  if yearinph==`y'
				}
			}
		}
		** 5 years before (to chck pretrends)
		forvalues x=1(1)5{
			g public_yearminus`x'=.
		}
		forvalues y=1988(1)2018{
			forvalues x= 1(1)5{
				local year=`y'-`x'
				if `year'>=1988{
					replace public_yearminus`x'=(public`year'==1) if yearinph==`y'
				}
			}
		}

		* PROBABILITY of changing jobs

		** Gen variables for income in each year w.r.t yearinph
		forvalues x=0(1)`max_time'{
			g changejob_year`x'=.
		}
		forvalues y=1989(1)2018{
			forvalues x= 0(1)`max_time'{
				local year=`y'+`x'
				local year_min1 = `year' -1
				if `y'<=(2016-`x'){
					replace changejob_year`x'=1 if ///
					(enterprise`year' != enterprise`year_min1') & ///
					(enterprise`year' != "" & enterprise`year_min1' != "") & ///
					yearinph==`y'
				}
			}
		}

		** calculate income in x years before (to chck pretrends)
		forvalues x=1(1)5{
			g changejob_yearminus`x'=.
		}

		forvalues y=1989(1)2018{
			forvalues x= 1(1)5{
				local year=`y'-`x'
				local year_min1= `y' - `x' -1
				if `year'>=1989{
					replace changejob_yearminus`x'=1 if ///
					(enterprise`year' != enterprise`year_min1') & ///
					(enterprise`year' != "" & enterprise`year_min1' != "") & ///
					yearinph==`y'
				}
			}
		}

		forvalues x=0(1)`max_time'{
			replace changejob_year`x' = 0 if changejob_year`x' == .
		}
		forvalues x=1(1)5{
			replace changejob_yearminus`x' = 0 if changejob_yearminus`x' == .
		}

		* TOTAL INCOME before/after hospitalization
		forvalues y=1987(1)2019{
			replace totalincome`y'=0 if totalincome`y'==. & occupational_status`y'==.
		}

		** Gen variables for total income in each year w.r.t yearinph
		forvalues x=0(1)`max_time'{
			g totalincome_year`x'=.
		}
		forvalues y=1987(1)2019{
			forvalues x= 0(1)`max_time'{
				local year=`y'+`x'
				if `y'<=(2018-`x'){
					replace totalincome_year`x'=totalincome`year' if yearinph==`y'
				}
			}
		}

		** calculate income in 5 years before (to check pretrends)
		forvalues x=1(1)5{
			g totalincome_yearminus`x'=.
		}
		forvalues y=1987(1)2019{
			forvalues x= 1(1)5{
				local year=`y'-`x'
				if `year'>=1987{
					replace totalincome_yearminus`x'=totalincome`year' if yearinph==`y'
				}
			}
		}

		* DISABILITY pension
		forvalues x=0(1)`max_time'{
			g disable_year`x'=.
		}

		forvalues y=1987(1)2019{
			forvalues x= 0(1)`max_time'{
			local year=`y'+`x'
				if `y'<=(2019-`x'){
					replace disable_year`x'=(disable`year'!="")  if yearinph==`y'
				}
			}
		}
		** 5 years before (to check pretrends)
		forvalues x=1(1)5{
			g disable_yearminus`x'=.
		} 
		forvalues y=1987(1)2019{
			forvalues x= 1(1)5{ 
			local year=`y'-`x'
				if `year'>=1987{
					replace disable_yearminus`x'=(disable`year'!="") if yearinph==`y'
				}
			}
		}

		* UNEMPLOYMENT 
		forvalues x=0(1)`max_time'{
			g unemp_year`x'=.
		}

		forvalues y=1987(1)2019{
			forvalues x= 0(1)`max_time'{
				local year=`y'+`x'
				if `y'<=(2019-`x'){
					replace unemp_year`x'=(unemp`year'!="")  if yearinph==`y'
				}
			}
		}

		** 5 years before (to check pretrends)
		forvalues x=1(1)5{
		g unemp_yearminus`x'=.
		}

		forvalues y=1987(1)2019{
			forvalues x= 1(1)5{
				local year=`y'-`x'
				if `year'>=1987{
					replace unemp_yearminus`x'=(unemp`year'!="") if yearinph==`y'
				}
			}
		}
	
		
		keep `did_vars' shnro_mother income_year* working* educ0 uni_educ0 ///
		divorced_year* married_year* unmarried_year* widowed_year* public_year* changejob_year* ///
		totalincome_year* disable_year* unemp_year* married_nokids* married_kids* mother_kids* father_kids* coh_commonkids* coh_nocommonkids* coh_nokids*

		lab var shnro_mother "mother id"
		save "$processed_data\mothers_income_diagnosis`name'.dta", replace 
		}

		
		{ // * FATHERS *

		* Merge individual information with treatment assignment
		use "$processed_data\fathers1988_2019_wide.dta", clear 
		cap drop _merge
		duplicates drop shnro_father, force
		** Match fathers with data on childhood shock 
		merge 1:m shnro_father using "$processed_data/`matchdata'.dta"
		drop if _merge==1
		drop _merge
		duplicates drop child_b_year shnro_mother, force
		if `eventstudy' != 1 {
			merge 1:m shnro_mother using "$processed_data/`dataset'.dta" 
			drop if _merge==1
			drop _merge
			** g year of treat (treatmeent year and placebo year for control units)
			g year_treat = child_b_year + ageinptreat
			g yearinph=year_treat

		}
		else {
			cap g yearinph=year(first_inphospital)
			cap g yearinph=year(first_mortality)
			
		}
		
		* INCOME before/after hospitalization
		** Set annual income to 0 if missing income and occupation
		forvalues y=1988(1)2018{
			replace income`y'=0 if income`y'==. & occupational_status`y'==.
		}
		** Gen variables for income in each year w.r.t yearinph
		forvalues x=0(1)`max_time'{
			g father_income_year`x'=.
		}
		forvalues y=1988(1)2017{
			forvalues x= 0(1)`max_time'{
			local year=`y'+`x'
			if `y'<=(2018-`x'){
				replace father_income_year`x'=income`year' if yearinph==`y'
				}
			}
		}

		** Calculate income in 5 years before (to check pretrends)
		forvalues x=1(1)5{
			g father_income_yearminus`x'=.
		}
		forvalues y=1988(1)2018{
			forvalues x= 1(1)5{
			local year=`y'-`x'
			if `year'>=1988{
				replace father_income_yearminus`x'=income`year' if yearinph==`y'
				}
			}
		}

		* EMPLOYMENT before/after hospitalization
		** calculate extensive margin variables: working if occupational_status==1 or 2
		forvalues x=0(1)`max_time'{
			g father_working_year`x'=.
		}
		forvalues y=1988(1)2017{
			forvalues x= 0(1)`max_time'{
				local year=`y'+`x'
				if `y'<=(2018-`x'){
					replace father_working_year`x'=(occupational_status`year'!=.) ///
					if yearinph==`y'
				}
			}
		}

		** Calculate 5 years before (to check pretrends)
		forvalues x=1(1)5{
			g father_working_yearminus`x'=.
		}
		forvalues y=1988(1)2018{
			forvalues x= 1(1)5{
				local year=`y'-`x'
				if `year'>=1988{
					replace father_working_yearminus`x'=(occupational_status`year'!=.) ///
					if yearinph==`y'
				}
			}
		}

		* EDUCATION before/after hospitalization
		** Clean
		forvalues y=1988(1)2018{
			g father_educ`y'=substr(ututku_aste`y',1,1)
			destring father_educ`y', replace
			gen father_uni_educ`y'=(father_educ`y'>=5) if  father_educ`y'!=. & father_educ`y'!=9
		}
		** Create var for level of education in the year of first hospital admission
		gen father_educ0=.
		gen father_uni_educ0=.
		forvalues y=1988(1)2018{
			replace father_educ0=father_educ`y' if yearinph==`y'
			replace father_uni_educ0=father_uni_educ`y' if yearinph==`y'
		}

		* PROBABILITY of DIVORCE
		forvalues x=0(1)`max_time'{
			g father_divorced_year`x'=.
		}

		forvalues y=1988(1)2019{
			forvalues x= 0(1)`max_time'{
				local year=`y'+`x'
				if `y'<=(2015-`x'){
					replace father_divorced_year`x'=(divorced`year'==1)  if yearinph==`y'
				}
			}
		}
				* PROBABILITY of MARRIED

			forvalues x=0(1)`max_time'{
			g father_married_year`x'=.
		}

		forvalues y=1988(1)2019{
			forvalues x= 0(1)`max_time'{
				local year=`y'+`x'
				if `y'<=(2015-`x'){
					replace father_married_year`x'=(married`year'==1)  if yearinph==`y'
				}
			}
		}
				* PROBABILITY of UNMARRIED

			forvalues x= 0(1)`max_time'{
    		g father_unmarried_year`x'=.
										
           }
        forvalues y=1988(1)2019{
		    	forvalues x= 0(1)`max_time'{
				 local year=`y'+`x'
				if `y'<=(2015-`x'){
					replace father_unmarried_year`x'=(unmarried`year'==1)  if yearinph==`y'
				}
			}
		}
				* PROBABILITY of WIDOWED

		
			forvalues x= 0(1)`max_time'{
				    		g father_widowed_year`x'=.
		}
		        forvalues y=1988(1)2019{
			forvalues x= 0(1)`max_time'{
				local year=`y'+`x'
				if `y'<=(2015-`x'){
					replace father_widowed_year`x'=(widowed`year'==1)  if yearinph==`y'
				}
			}
		}
		** 5 years before (to check pretrends)
		forvalues x=1(1)5{
			g father_divorced_yearminus`x'=.
		}
		forvalues y=1988(1)2019{
			forvalues x= 1(1)5{
				local year=`y'-`x'
				if `year'>=1988{
					replace father_divorced_yearminus`x'=(divorced`year'==1) if yearinph==`y'
				}
			}
		}
		
				forvalues x=1(1)5{
			g father_married_yearminus`x'=.
		}
		forvalues y=1988(1)2019{
			forvalues x= 1(1)5{
				local year=`y'-`x'
				if `year'>=1988{
					replace father_married_yearminus`x'=(married`year'==1) if yearinph==`y'
				}
			}
		}
		
				forvalues x=1(1)5{
			g father_unmarried_yearminus`x'=.
		}
		forvalues y=1988(1)2019{
			forvalues x= 1(1)5{
				local year=`y'-`x'
				if `year'>=1988{
					replace father_unmarried_yearminus`x'=(unmarried`year'==1) if yearinph==`y'
				}
			}
		}
		
			forvalues x=1(1)5{
			g father_widowed_yearminus`x'=.
		}
		forvalues y=1988(1)2019{
			forvalues x= 1(1)5{
				local year=`y'-`x'
				if `year'>=1988{
					replace father_widowed_yearminus`x'=(widowed`year'==1) if yearinph==`y'
				}
			}
		}	
		

		* PROBABILITY of WORKING in the public sector
		forvalues x=0(1)`max_time'{
			g father_public_year`x'=.
		}
		forvalues y=1988(1)2018{
			forvalues x= 0(1)`max_time'{
				local year=`y'+`x'
				if `y'<=(2015-`x'){
					replace father_public_year`x'=(public`year'==1)  if yearinph==`y'
				}
			}
		}
		** 5 years before (to chck pretrends)
		forvalues x=1(1)5{
			g father_public_yearminus`x'=.
		}
		forvalues y=1988(1)2018{
			forvalues x= 1(1)5{
				local year=`y'-`x'
				if `year'>=1988{
					replace father_public_yearminus`x'=(public`year'==1) if yearinph==`y'
				}
			}
		}

		* PROBABILITY of changing jobs
		** Gen variables for income in each year w.r.t yearinph
		forvalues x=0(1)`max_time'{
			g father_changejob_year`x'=.
		}
		forvalues y=1989(1)2018{
			forvalues x= 0(1)`max_time'{
				local year=`y'+`x'
				local year_min1 = `year' -1
				if `y'<=(2016-`x'){
					replace father_changejob_year`x'=1 if ///
					(enterprise`year' != enterprise`year_min1') & ///
					(enterprise`year' != "" & enterprise`year_min1' != "") & ///
					yearinph==`y'
				}
			}
		}

		** calculate income in x years before (to chck pretrends)
		forvalues x=1(1)5{
			g father_changejob_yearminus`x'=.
		}

		forvalues y=1989(1)2018{
			forvalues x= 1(1)5{
				local year=`y'-`x'
				local year_min1= `y' - `x' -1
				if `year'>=1989{
					replace father_changejob_yearminus`x'=1 if ///
					(enterprise`year' != enterprise`year_min1') & ///
					(enterprise`year' != "" & enterprise`year_min1' != "") & ///
					yearinph==`y'
				}
			}
		}

		forvalues x=0(1)`max_time'{
			replace father_changejob_year`x' = 0 if father_changejob_year`x' == .
		}
		forvalues x=1(1)5{
			replace father_changejob_yearminus`x' = 0 if father_changejob_yearminus`x' == .
		}


		* TOTAL INCOME before/after hospitalization
		forvalues y=1987(1)2019{
			replace totalincome`y'=0 if totalincome`y'==. & occupational_status`y'==.
		}

		** Gen variables for total income in each year w.r.t yearinph
		forvalues x=0(1)`max_time'{
			g f_totalincome_year`x'=.
		}
		forvalues y=1987(1)2019{
			forvalues x= 0(1)`max_time'{
				local year=`y'+`x'
				if `y'<=(2018-`x'){
					replace f_totalincome_year`x'=totalincome`year' if yearinph==`y'
				}
			}
		}

		** Calculate income in 5 years before (to check pretrends)
		forvalues x=1(1)5{
			g f_totalincome_yearminus`x'=.
		}
		forvalues y=1987(1)2019{
			forvalues x= 1(1)5{
				local year=`y'-`x'
				if `year'>=1987{
					replace f_totalincome_yearminus`x'=totalincome`year' if yearinph==`y'
				}
			}
		}

		* DISABILITY pension
		forvalues x=0(1)`max_time'{
			g father_disable_year`x'=.
		}

		forvalues y=1987(1)2019{
			forvalues x= 0(1)`max_time'{
			local year=`y'+`x'
				if `y'<=(2019-`x'){
					replace father_disable_year`x'=(disable`year'!="")  if yearinph==`y'
				}
			}
		}
		** 5 years before (to check pretrends)
		forvalues x=1(1)5{
			g father_disable_yearminus`x'=.
		}
		forvalues y=1987(1)2019{
			forvalues x= 1(1)5{
			local year=`y'-`x'
				if `year'>=1987{
					replace father_disable_yearminus`x'=(disable`year'!="") ///
					if yearinph==`y'
				}
			}
		}

		* UNEMPLOYMENT pension
		forvalues x=0(1)`max_time'{
			g father_unemp_year`x'=.
		}

		forvalues y=1987(1)2019{
			forvalues x= 0(1)`max_time'{
				local year=`y'+`x'
				if `y'<=(2018-`x'){
					replace father_unemp_year`x'=(unemp`year'!="")  if yearinph==`y'
				}
			}
		}

		* x years before (to check pretrends)
		forvalues x=1(1)5{
			g father_unemp_yearminus`x'=.
		}

		forvalues y=1987(1)2019{
			forvalues x= 1(1)5{
				local year=`y'-`x'
				if `year'>=1987{
					replace father_unemp_yearminus`x'=(unemp`year'!="") if yearinph==`y'
				}
			}
		}


		* AGE of FATHER at admission 
		gen age_father_inp=.
		forvalues y=1988(1)2018{
			replace age_father_inp=age`y' if yearinph==`y'
		}
		** use surrounding years to complete missings
		forvalues y = 1988(1)2017{
			forvalues x=-25(1)25{
				local year=`y'+`x'
				if `year'>=1988 & `year'<=2018{
					replace age_father_inp=age`year'-`x' if ///
					yearinph==`y' & age_father_inp==.
				}
			}
		}


		keep `did_vars' shnro_father shnro_mother father_income_year* age_father_inp ///
		father_working* father_educ0 father_uni_educ0 ///
		father_divorced_year* father_married_year* father_unmarried_year* father_widowed_year* father_public_year* father_changejob_year* /// 
		f_totalincome_year* father_disable_year* father_unemp_year*

		save "$processed_data/fathers_income_diagnosis`name'.dta", replace 
		}
	}

	if `additional_outcomes' == 1 { // Outcomes: Mental health, transfers, and allowances
	  
		{ // * Mental health diagnoses *
		* MOTHERS *
		use "$processed_data/`dataset'.dta", clear
		if `eventstudy' != 1 {
			** generate year of treat (treatmeent year and placebo year for control units)
			g year_treat = child_b_year + ageinptreat
			g yearinph=year_treat
			
			merge m:1 shnro_mother using "$processed_data/mentalhealthmom.dta"
			drop if _merge==2 // drop if it is a health diagnosis for a someonee not in our mother sample
			drop _merge
			
		
			local did_vars treat
		}
		else {
			cap g yearinph=year(first_inphospital)
			cap g yearinph=year(first_mortality)
			
			merge 1:1 shnro_mother using "$processed_data/mentalhealthmom.dta"
			drop if _merge == 2 
			drop _merge 
			
			local did_vars
			
		}
		
		foreach y of numlist 1986(1)2017{
			replace n_diag_year`y'=0 if n_diag_year`y'==.
		}

		** same as income data: clean mental health five years and ten years after health shock 
		forvalues x=0(1)4{
			g mentalhealthyear`x'=.
		}

		forvalues y=1986(1)2017{
			forvalues x= 0(1)4{
				local year=`y'+`x'
				if `y'<=(2017-`x'){
					replace mentalhealthyear`x'=n_diag_year`year' if yearinph==`y'
				}
			}
		}

		* calculate mhealth visits in x years before (to check pretrends)
		forvalues x=1(1)5{
			g mentalhealthyearminus`x'=.
		}

		forvalues y=1986(1)2017{
			forvalues x= 1(1)5{
				local year=`y'-`x'
				if `year'>=1986{
					replace mentalhealthyearminus`x'=n_diag_year`year' if yearinph==`y'
				}
			}
		}

		keep `did_vars' shnro_mother mentalhealth*
		save "$processed_data/mentalhealth.dta",replace 

		use "$processed_data/mothers_income_diagnosis`name'.dta", clear 
		if `eventstudy' != 1 {
			merge 1:1 shnro_mother treat using "$processed_data/mentalhealth.dta", ///
			gen(mh_merge)
			*drop if _merge == 2
			*drop _merge
		}
		else {
			merge 1:1 shnro_mother using "$processed_data/mentalhealth.dta", ///
			gen(mh_merge)
		}
		
		save "$processed_data/mothers_income_diagnosis`name'.dta", replace 
		
		* FATHERS *
		use "$processed_data/`dataset'.dta", clear 
		if `eventstudy' != 1 {
			** generate year of treat (treatmeent year and placebo year for control units)
			g year_treat = child_b_year + ageinptreat
			g yearinph=year_treat
		
			local did_vars treat
		}
		
		else {
			cap g yearinph=year(first_inphospital)
			cap g yearinph=year(first_mortality)
	
			local did_vars
		}
		
		
		merge m:1 shnro_father using "$processed_data/mentalhealthdad.dta"
		drop if _merge==2 // drop if it is a health diagnosis for a someonee not in our mother sample
		drop _merge
		
		foreach y of numlist 1986(1)2017{
			replace n_diag_year`y'=0 if n_diag_year`y'==.
		}

		** same as income data: clean mental health five years and ten years after health shock 
		forvalues x=0(1)4{
			g mentalhealthyear`x'=.
		}

		forvalues y=1986(1)2017{
			forvalues x= 0(1)4{
				local year=`y'+`x'
				if `y'<=(2017-`x'){
					replace mentalhealthyear`x'=n_diag_year`year' if yearinph==`y'
				}
			}
		}

		** calculate mhealth visits in x years before (to check pretrends)
		forvalues x=1(1)5{
			g mentalhealthyearminus`x'=.
		}

		forvalues y=1986(1)2017{
			forvalues x= 1(1)5{
				local year=`y'-`x'
				if `year'>=1986{
					replace mentalhealthyearminus`x'=n_diag_year`year' if yearinph==`y'
				}
			}
		}

		rename mentalhealth* mentalhealthdad*
		keep `did_vars' shnro_mother shnro_father mentalhealth* 
		save "$processed_data/mentalhealth.dta",replace 

		use "$processed_data/fathers_income_diagnosis`name'.dta", clear
		if `eventstudy' != 1 {
			merge 1:1 shnro_mother shnro_father treat using ///
			"$processed_data/mentalhealth.dta", gen(mh_merge_dad)
			*drop if _merge == 2
			*drop _merge
		}
		else {
			merge 1:1 shnro_mother shnro_father using ///
			"$processed_data/mentalhealth.dta", gen(mh_merge_dad)
		}

		save "$processed_data/fathers_income_diagnosis`name'.dta", replace 
		
		}
		{ // * Transfers *
		
		* MOTHERS *
		use "$processed_data/`dataset'.dta", clear 
		if `eventstudy' != 1 {
			** generate year of treat (treatmeent year and placebo year for control units)
			g year_treat = child_b_year + ageinptreat
			g yearinph=year_treat
			merge m:1 shnro_mother using ///
			"$processed_data\transfers_mothers1988_2019_wide.dta"
			
			local did_vars treat
		}
		else {
			cap g yearinph=year(first_inphospital)
			cap g yearinph=year(first_mortality)
			merge 1:1 shnro_mother using ///
			"$processed_data\transfers_mothers1988_2019_wide.dta"

		}

		drop if _merge==2 // drop if it is a health diagnosis for a someonee not in our mother sample
		drop _merge

		** Gen variables for transfers in each year w.r.t yearinph
		forvalues x=0(1)`max_time'{
			g transfers_year`x'=.
		}

		forvalues y=1988(1)2017{
			forvalues x= 0(1)`max_time'{
				local year=`y'+`x'
				if `y'<=(2018-`x'){
					replace transfers_year`x'=transfers`year' if yearinph==`y'
				}
			}
		}

		* calculate transfers in x years before (to check pretrends)
		forvalues x=1(1)5{
			g transfers_yearminus`x'=.
		}

		forvalues y=1988(1)2018{
		forvalues x= 1(1)5{
				local year=`y'-`x'
				if `year'>=1988{
					replace transfers_yearminus`x'=transfers`year' if yearinph==`y'
				}
			}
		}

		keep `did_vars' shnro_mother transfers_year*
		lab var shnro_mother "mother id"
		save "$processed_data/transfers.dta", replace

		use "$processed_data/mothers_income_diagnosis`name'.dta", clear
		if `eventstudy' != 1 {
			merge 1:1 shnro_mother treat using "$processed_data/transfers.dta", ///
			gen(transfers_merge)
		}
		else {
			merge 1:1 shnro_mother using "$processed_data/transfers.dta", ///
			gen(transfers_merge)
		}

		save "$processed_data/mothers_income_diagnosis`name'.dta", replace 

		* FATHERS *
		** Merge with matched.dta and delta = 4 treatment
		use "$processed_data/`dataset'.dta", clear 
		if `eventstudy' != 1 {
			** generate year of treat (treatmeent year and placebo year for control units)
			g year_treat = child_b_year + ageinptreat
			g yearinph=year_treat
			
			local did_vars treat
		}
		else {
			cap g yearinph=year(first_inphospital)
			cap g yearinph=year(first_mortality)
			
			local did_vars 
		}
		merge m:1 shnro_father using "$processed_data\transfers_fathers1988_2019_wide.dta"
		drop if _merge == 2
		drop _merge 

		*Gen variables for transfers in each year w.r.t yearinph
		forvalues x=0(1)`max_time'{
			g father_transfers_year`x'=.
		}

		forvalues y=1988(1)2017{
			forvalues x= 0(1)`max_time'{
				local year=`y'+`x'
				if `y'<=(2018-`x'){
					replace father_transfers_year`x'=transfers`year' if yearinph==`y'
				}
			}
		}

		* calculate transfers in x years before (to check pretrends)
		forvalues x=1(1)5{
			g father_transfers_yearminus`x'=.
		}

		forvalues y=1988(1)2018{
			forvalues x= 1(1)5{
				local year=`y'-`x'
				if `year'>=1988{
					replace father_transfers_yearminus`x'=transfers`year' if yearinph==`y'
				}
			}
		}

		keep `did_vars' shnro_father shnro_mother father_transfers_year*

		save "$processed_data/transfers.dta", replace

		use "$processed_data/fathers_income_diagnosis`name'.dta", clear 
		if `eventstudy' != 1 {
			merge 1:1 shnro_mother shnro_father treat using ///
			"$processed_data/transfers.dta", gen(transfers_merge_dad)
		}
		else {
			merge 1:1 shnro_mother shnro_father using ///
			"$processed_data/transfers.dta", gen(transfers_merge_dad)
		}
		save "$processed_data/fathers_income_diagnosis`name'.dta", replace

		}
		{ // * Allowances *

		* MOTHERS *
		use "$processed_data/`dataset'.dta", clear
		if `eventstudy' != 1 {
			** generate year of treat (treatmeent year and placebo year for control units)
			g year_treat = child_b_year + ageinptreat
			g yearinph=year_treat
			merge m:1 shnro_mother using ///
		"$processed_data\family_allowance_mothers1988_2019_wide.dta"
			
			local did_vars treat
		}
		else {
			cap g yearinph=year(first_inphospital)
			cap g yearinph=year(first_mortality)
			merge 1:1 shnro_mother using ///
			"$processed_data\family_allowance_mothers1988_2019_wide.dta"

		}
		drop if _merge==2
		drop _merge

		** gen variables for transfers in each year w.r.t yearinph
		forvalues x=0(1)`max_time'{
			g fam_allow_year`x'=.
		}

		forvalues y=1988(1)2017{
			forvalues x= 0(1)`max_time'{
				local year=`y'+`x'
				if `y'<=(2018-`x'){
					replace fam_allow_year`x'=family_allowance`year' if yearinph==`y'
				}
			}
		}

		* calculate transfers in x years before (to check pretrends)
		forvalues x=1(1)5{
			g fam_allow_yearminus`x'=.
		}

		forvalues y=1988(1)2018{
			forvalues x= 1(1)5{
				local year=`y'-`x'
				if `year'>=1988{
					replace fam_allow_yearminus`x'=family_allowance`year' if yearinph==`y'
				}
			}
		}

		keep `did_vars' shnro_mother fam_allow_year*
		lab var shnro_mother "mother id"
		save "$processed_data/allowances.dta", replace 

		use "$processed_data/mothers_income_diagnosis`name'.dta", clear 
		
		if `eventstudy' != 1 {
			merge 1:1 shnro_mother treat using "$processed_data/allowances.dta", ///
			gen(allowances_merge)
		}
		else {
			merge 1:1 shnro_mother using "$processed_data/allowances.dta", ///
			gen(allowances_merge)
		}
		
		save "$processed_data/mothers_income_diagnosis`name'.dta", replace 

		* FATHERS *
		** Merge with matched.dta and delta = 4 treatment
		use "$processed_data/`dataset'.dta", clear 
		if `eventstudy' != 1 {
			** generate year of treat (treatmeent year and placebo year for control units)
			g year_treat = child_b_year + ageinptreat
			g yearinph=year_treat
			
			local did_vars treat
		}
		else {
			cap g yearinph=year(first_inphospital)
			cap g yearinph=year(first_mortality)
			
			local did_vars
		}
		merge m:1 shnro_father using ///
		"$processed_data/family_allowance_fathers1988_2019_wide.dta"
		drop if _merge == 2
		drop _merge

		** gen variables for transfers in each year w.r.t yearinph
		forvalues x=0(1)`max_time'{
			g f_fam_allow_year`x'=.
		}

		forvalues y=1988(1)2017{
			forvalues x= 0(1)`max_time'{
			local year=`y'+`x'
				if `y'<=(2018-`x'){
					replace f_fam_allow_year`x'=family_allowance`year' if yearinph==`y'
				}
			}
		}

		* calculate transfers in x years before (to check pretrends)
		forvalues x=1(1)5{
			g f_fam_allow_yearminus`x'=.
		}

		forvalues y=1988(1)2018{
			forvalues x= 1(1)5{
				local year=`y'-`x'
				if `year'>=1988{
					replace f_fam_allow_yearminus`x'=family_allowance`year' if yearinph==`y'
				}
			}
		}

		keep `did_vars' shnro_father shnro_mother f_fam_allow_year*

		save "$processed_data/allowances.dta", replace
		 
		use "$processed_data/fathers_income_diagnosis`name'.dta", clear 
		
		if `eventstudy' != 1 {
			merge 1:1 shnro_mother shnro_father treat using ///
			"$processed_data/allowances.dta", gen(allowances_merge_dad)
		}
		
		else {
			merge 1:1 shnro_mother shnro_father using ///
			"$processed_data/allowances.dta", gen(allowances_merge_dad)
		}
	

		save "$processed_data/fathers_income_diagnosis`name'.dta", replace
		}
	
	}
}

timer off 1
timer list // 6228.1970 ~ 1hr 40 min

*********************************************************
* Final Cleaning for Summary Statistics
*********************************************************

* All mothers : mothers_income_diagnosis4 mothers_income_diagnosis_all  mothers_income_diagnosis4_mortality mothers_income_diagnosis_mortality_all

* All fathers : fathers_income_diagnosis4 fathers_income_diagnosis_all fathers_income_diagnosis4_mortality fathers_income_diagnosis_mortality_all

global datasets_mother "mothers_income_diagnosis4 mothers_income_diagnosis_all  mothers_income_diagnosis4_mortality mothers_income_diagnosis_mortality_all"

global datasets_father "fathers_income_diagnosis4 fathers_income_diagnosis_all fathers_income_diagnosis4_mortality fathers_income_diagnosis_mortality_all"

foreach dataset of global datasets_mother {
	use "$processed_data/`dataset'.dta", clear 
		
	** Education degrees 
	cap tab educ0, gen(educ)
	label var educ1 "Upper secondary level mother" 
	label var educ2 "Post-secondary non-tertiary education mother"
	label var educ3 "Short-cycle tertiary education mother"
	label var educ4 "Bachelor's or equivalent level mother"
	label var educ5 "Master's or equivalent level mother"
	label var educ6 "Doctoral or equivalent level mother"
	** Mother's income 
	label var income_yearminus2 "Earnings mother t=-2"
	** Mother's employment
	cap gen working2 = (income_yearminus2 > 0) if !missing(income_yearminus2)
	label var working2 "Prob. working mother t=-2"
	** Mother's unemployment
	label var unemp_yearminus2 "Prob. unemployed mother t=-2"
	** Mother total income
	label var totalincome_yearminus2 "Total earnings mother t=-2"
	** Mother's mental health visits (N)
	label var mentalhealthyearminus2 "N visits mental health mother t-2"
	*** Prob mother mental health visit
	cap gen mentalhealthyearminus2_any = (mentalhealthyearminus2 >0) if ///
	!missing(mentalhealthyearminus2)
	label var mentalhealthyearminus2_any "Prob. mental health visit mother t-2"
	** Employed in public sector 
	label var public_yearminus2 "Prob. working in the public sector mother t-2"
	** Changed job 
	label var changejob_yearminus2 "Prob. changing job mother t-2"
	** Divorced
	label var divorced_yearminus2 "Prob. divorced t-2"
	** Married
	label var married_yearminus2 "Prob. married t-2"
	** Unmarried
    label var unmarried_yearminus2 "Prob. unmarried t-2"
	** Widowed
	label var widowed_yearminus2 "Prob. widowed t-2"
	** Family status variables 
    label var mother_kids_yearminus2 "Prob. mother with kids t-2"

	
	
	save "$processed_data/`dataset'.dta", replace 
}

foreach dataset of global datasets_father {
	use "$processed_data/`dataset'.dta", clear
	
	** Education degrees 
	cap tab father_educ0, gen(father_educ)
	label var father_educ1 "Upper secondary level father" 
	label var father_educ2 "Post-secondary non-tertiary education father"
	label var father_educ3 "Short-cycle tertiary education father"
	label var father_educ4 "Bachelor's or equivalent level father"
	label var father_educ5 "Master's or equivalent level father"
	label var father_educ6 "Doctoral or equivalent level father"
	** Father's income 
	label var father_income_yearminus2 "Earnings father t=-2"
	** Father's employment
	cap gen f_working2 = (father_income_yearminus2 > 0) if !missing(father_income_yearminus2)
	label var f_working2 "Prob. working father t=-2"
	** Father's unemployment
	label var father_unemp_yearminus2 "Prob. unemployed father t=-2"
	** Father's total income
	label var f_totalincome_yearminus2 "Total earnings father t=-2"
	** Father's mental health visits (N)
	label var mentalhealthdadyearminus2 "N visits mental health father t-2"
	*** Prob father mental health visit
	cap gen mentalhealthdadyearminus2_any = (mentalhealthdadyearminus2 >0) if ///
	!missing(mentalhealthdadyearminus2)
	label var mentalhealthdadyearminus2_any "Prob. mental health visit father t-2"
	** Employed in public sector 
	label var father_public_yearminus2 "Prob. working in the public sector father t-2"
	** Changed job 
	label var father_changejob_yearminus2 "Prob. changing job father t-2"
	
	** Age of father in hospital
	label var age_father_inp "Age father at admission"
	
	save "$processed_data/`dataset'.dta", replace 
	
}

*********************************************************
* Create Analytical Datasets and Samples Corresponding to Main Regressions
*********************************************************

global sample_did !missing(father_income) & !missing(income) & ///
!missing(treat) & !missing(age_mother) & ///
!missing(age_father) & !missing(year_calendar) & ///
!missing(child_b_year) & !missing(educ0) & ///
!missing(father_educ0) & !missing(male) & ///
inrange(yearinp,1995,2014) 

global sample_did_mortality !missing(father_income) & !missing(income) & ///
!missing(treat) & !missing(age_mother) & ///
!missing(age_father) & !missing(year_calendar) & ///
!missing(child_b_year) & !missing(male) & inrange(yearinp,1995,2014) 


/*
There should not be any issues remerging each mother has a unique value for yearinp, 
whereas this is not the case for ageinptreat. There, a mother can have up to 2 values, one for there actual year of treatment, and the year of treatment for which they act as control (if they act as control too). 
*/



global sample_event !missing(father_income) & !missing(income) ///
& !missing(year_calendar) & inrange(age_firstinphospital,7,18) & inrange(yearinp,1995,2014) 

global sample_event_mortality !missing(father_income) & !missing(income) ///
& !missing(year_calendar) & inrange(age_firstmortality,7,18) & inrange(yearinp,1995,2014) 

*****************************
* DiD Hospitalization Sample
*****************************
use "$processed_data\date_diagnosis.dta", clear 
merge 1:m shnro_mother using "$processed_data\mothers_income_diagnosis4", keep(3) nogen 
merge 1:1 shnro_mother treat using "$processed_data\fathers_income_diagnosis4", keep(3) nogen

keep yearinp shnro_mother shnro_father age_mother_inp age_father_inp income_* ///
father_income_* educ0 child_b_year father_educ0 male treat ageinptreat year_treat nchild_year*

egen new_shnromother = group(shnro_mother treat)
reshape long income_ father_income_ nchild_, ///
i(new_shnromother) j(year_event) string
drop new_shnromother
rename income_ income
rename father_income_ father_income
rename nchild_ nchild
 
* gen numeric variable for time w.r.t  to the hospitalization
gen time_event=.
forvalues x=0(1)3{
	replace time_event=`x' if year_event=="year`x'"
}
forvalues x=1(1)5{
	replace time_event=-`x' if year_event=="yearminus`x'"
}

* gen year of income (calendar year)
gen year_calendar=year_treat + time_event

* gen age of mother in each year
gen mother_b_year=year_treat - age_mother_inp
gen age_mother=year_calendar - mother_b_year

* gen age of father in each year
gen father_b_year=year_treat - age_father_inp
gen age_father=year_calendar - father_b_year

* dummify time_event, with -2 as omitted variable
** all but i_time_even_4 (-2 time_event)
xi i.time_event,prefix("i_") noomit
global event_dummies i_time_even_1 i_time_even_2 i_time_even_3 i_time_even_5 ///
i_time_even_6 i_time_even_7 i_time_even_8 i_time_even_9

* g interactions 
local time_to_shock = -5 
forvalues i= 1(1)9{
	g even`i'_treat = i_time_even_`i'*treat
	label var even`i'_treat "`time_to_shock'"
	local ++time_to_shock 
}

** all but i_time_even_5 (-1 time_event)
global event_treat_dummies even1_treat even2_treat even3_treat even5_treat ///
even6_treat even7_treat even8_treat even9_treat

forvalues i= 1(1)3{
	local x = `i' + 6 
	label var i_time_even_`x' "`i' years after hospitalization" 
}
forvalues i= 1(1)5 {
	local x =  6 - `i'
	label var i_time_even_`i' "`x' years before hospitalization"
}

label var i_time_even_6 "Year of hospitalization" 

** Set controls
keep if $sample_did
gen sample_hospital = 1 
duplicates drop shnro_mother, force

keep shnro_mother sample_hospital
save "$processed_data\sample.dta", replace 

use "$processed_data\date_diagnosis.dta", clear 
merge 1:1 shnro_mother using "$processed_data\sample.dta"
drop _merge 
replace sample_hospital = 0 if sample_hospital == .
tab sample_hospital, m 
save "$processed_data\date_diagnosis.dta", replace

*****************************
* DiD Mortality Sample *
*****************************
use "$processed_data\date_mortality.dta", clear 
merge 1:m shnro_mother using ///
"$processed_data\mothers_income_diagnosis4_mortality", keep(3) nogen 
merge 1:1 shnro_mother treat using ///
"$processed_data\fathers_income_diagnosis4_mortality", keep(3) nogen 
 
* Put in long format
keep yearinp shnro_mother shnro_father age_mother_inp age_father_inp income_* ///
father_income_* educ0 child_b_year father_educ0 male treat ageinptreat year_treat nchild_year*

egen new_shnromother = group(shnro_mother treat)
reshape long income_ father_income_ nchild_ ,i(new_shnromother) j(year_event) string
rename income_ income
rename father_income_ father_income
rename nchild_ nchild

* gen time event 
gen time_event=.
forvalues x=0(1)3{
	replace time_event=`x' if year_event=="year`x'"
}
forvalues x=1(1)5{
	replace time_event=-`x' if year_event=="yearminus`x'"
}

* gen year of income (calendar year)
gen year_calendar=year_treat+time_event

* gen age of mother in each year
gen mother_b_year=year_treat - age_mother_inp
gen age_mother=year_calendar-mother_b_year

* gen age of father in each year
gen father_b_year=year_treat - age_father_inp
gen age_father=year_calendar - father_b_year

* dummify time_event, with -2 as omitted variable
** all but i_time_even_4 (-2 time_event)
xi i.time_event,prefix("i_") noomit
global event_dummies i_time_even_1 i_time_even_2 i_time_even_3 i_time_even_5 ///
i_time_even_6 i_time_even_7 i_time_even_8 i_time_even_9

* g interactions 
local time_to_shock = -5 
forvalues i= 1(1)9{
	g even`i'_treat = i_time_even_`i'*treat
	label var even`i'_treat "`time_to_shock'"
	local ++time_to_shock 
}

** all but i_time_even_5 (-1 time_event)
global event_treat_dummies even1_treat even2_treat even3_treat even5_treat ///
even6_treat even7_treat even8_treat even9_treat

forvalues i= 1(1)3{
	local x = `i' + 6 
	label var i_time_even_`x' "`i' years after hospitalization" 
}
forvalues i= 1(1)5 {
	local x =  6 - `i'
	label var i_time_even_`i' "`x' years before hospitalization"
}

label var i_time_even_6 "Year of hospitalization" 

keep if $sample_did_mortality

gen sample_mortality = 1
duplicates drop shnro_mother, force 
keep shnro_mother sample_mortality
save "$processed_data\sample.dta", replace 

use "$processed_data\date_mortality.dta", clear 
merge 1:1 shnro_mother using "$processed_data\sample.dta", nogen
replace sample_mortality = 0 if sample_mortality == . 
tab sample_mortality, m 
save "$processed_data\date_mortality.dta", replace


*****************************
* Event Study Hospitalization
*****************************

use "$processed_data\date_diagnosis.dta", clear
** merge mothers income
merge 1:1 shnro_mother using "$processed_data\mothers_income_diagnosis_all", keep(3) nogen
** merge fathers income
merge 1:1 shnro_mother using "$processed_data\fathers_income_diagnosis_all", keep(3) nogen

keep yearinp shnro_mother shnro_father ///
income_* father_income_* age_firstinphospital

reshape long income_ father_income_,i(shnro_mother) j(year_event) string
rename income_ income
rename father_income_ father_income

** gen numeric variable for time w.r.t health shock (health shock year==0)
gen time_event=.
forvalues x=0(1)3{
replace time_event=`x' if year_event=="year`x'"
}
forvalues x=1(1)5{
replace time_event=-`x' if year_event=="yearminus`x'"
}

** gen year of income (calendar year)
gen year_calendar=yearinp+time_event

* dummify time_event, with -2 as omitted variable
** all but i_time_even_4 (-2 time_event)
xi i.time_event,prefix("i_") noomit
global event_dummies i_time_even_2 i_time_even_3 i_time_even_5 ///
i_time_even_6 i_time_even_7 i_time_even_8 i_time_even_9

forvalues i= 1(1)3{
local x = `i' + 6 
label var i_time_even_`x' "`i' years after hospitalization" 
}
forvalues i= 1(1)5 {
local x =  6 - `i'
label var i_time_even_`i' "`x' years before hospitalization"
}

label var i_time_even_6 "Year of hospitalization"

keep if $sample_event

** Generate numeric ids to control for individual fixed effects
count if missing(shnro_mother)
encode shnro_mother, g (mother_id)
drop if missing(shnro_father)
encode shnro_father, g(father_id)

gen sample_hospital_event = 1 
duplicates drop shnro_mother, force 
keep shnro_mother sample_hospital_event
save "$processed_data\sample.dta", replace 

use "$processed_data\date_diagnosis.dta", clear 
merge 1:1 shnro_mother using "$processed_data\sample.dta"
drop _merge 
replace sample_hospital_event = 0 if sample_hospital_event == . 
tab sample_hospital_event, m 
save "$processed_data\date_diagnosis.dta", replace

*****************************
* Event Study Mortality
*****************************
use "$processed_data\date_mortality.dta", clear

** merge mothers income
merge 1:1 shnro_mother using "$processed_data\mothers_income_diagnosis_mortality_all", keep(3) nogen

** merge fathers income
merge 1:1 shnro_mother using "$processed_data\fathers_income_diagnosis_mortality_all", keep(3) nogen 

keep yearinp shnro_mother shnro_father age_mother_inp ///
income_* father_income_* educ0 child_b_year male age_firstmortality

reshape long income_ father_income_,i(shnro_mother) j(year_event) string
rename income_ income
rename father_income_ father_income

** gen numeric variable for time w.r.t health shock (health shock year==0)
gen time_event=.
forvalues x=0(1)3{
replace time_event=`x' if year_event=="year`x'"
}
forvalues x=1(1)5{
replace time_event=-`x' if year_event=="yearminus`x'"
}

** gen year of income (calendar year)
gen year_calendar=yearinp+time_event

** gen age of mother in each year
gen mother_b_year=yearinp - age_mother_inp
gen age_mother=year_calendar-mother_b_year

** gen age of child in each year
gen age_child = year_calendar - child_b_year

* dummify time_event, with -2 as omitted variable
** all but i_time_even_4 (-2 time_event)
xi i.time_event,prefix("i_") noomit
global event_dummies i_time_even_2 i_time_even_3 i_time_even_5 ///
i_time_even_6 i_time_even_7 i_time_even_8 i_time_even_9

forvalues i= 1(1)3{
local x = `i' + 6 
label var i_time_even_`x' "`i' years after hospitalization" 
}
forvalues i= 1(1)5 {
local x =  6 - `i'
label var i_time_even_`i' "`x' years before hospitalization"
}

label var i_time_even_6 "Year of hospitalization"

keep if $sample_event_mortality

** Generate numeric ids to control for individual fixed effects
count if missing(shnro_mother)
encode shnro_mother, g (mother_id)
drop if missing(shnro_father)
encode shnro_father, g(father_id)

gen sample_mortality_event = 1 
duplicates drop shnro_mother, force 
keep shnro_mother sample_mortality_event
save "$processed_data\sample.dta", replace 

use "$processed_data\date_mortality.dta", clear 
merge 1:1 shnro_mother using "$processed_data\sample.dta"
drop _merge 
replace sample_mortality_event = 0 if sample_mortality_event == . 
tab sample_mortality_event, m 
tab sample_mortality, m 
save "$processed_data\date_mortality.dta", replace

